package com.kb5173.search.web.action.impl;

import com.kb5173.framework.common.AbstractBaseService;
import com.kb5173.framework.common.IServiceResponse;
import com.kb5173.framework.dataaccess.pagination.GenericPage;
import com.kb5173.framework.logging.enums.LogCategory;
import com.kb5173.framework.shared.exception.BusinessException;
import com.kb5173.framework.shared.utils.BeanMapper;
import com.kb5173.search.common.entity.GoodsEO;
import com.kb5173.search.index.service.IIndexService;
import com.kb5173.search.web.action.ISearchAction;
import com.kb5173.search.words.service.IWordsService;
import com.kb5173.search.web.action.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 * @author Chengfei.Sun
 */
@Service("SearchService")
@Path("search")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class SearchActionImpl extends AbstractBaseService implements ISearchAction {
    @Autowired
    IIndexService indexManager;

    @Autowired
    IWordsService wordsManager;

    @Path("query")
    @POST
    @Override
    public IServiceResponse search(SearchRequest request, HttpServletRequest servletRequest) {
        logger.debug(LogCategory.SEARCH, "搜索请求信息:{}", request);

        // 初始化返回数据
        SearchResponse response = new SearchResponse();
        try {
            GenericPage<GoodsEO> result = indexManager.search(request.getGameName(), request.getGameArea(),
                    request.getGameServer(), request.getType(),
                    request.getKeyword(), request.getPageSize(), request.getPageNub());
            response.setGoodsPage(result);
            this.success(response);
        } catch (BusinessException e) {
            logger.error(LogCategory.SEARCH, "搜索发生异常:{}", e);
            this.error(response, e);
        }
        logger.debug(LogCategory.SEARCH, "搜索响应信息:{}",response);
        return response;
    }

    @Path("addwords")
    @POST
    @Override
    public IServiceResponse addWords(WordsRequest request, HttpServletRequest servletRequest) {
        logger.debug(LogCategory.SEARCH, "添加分词请求信息:{}", request);

        // 初始化返回数据
        GeneralResponse response = new GeneralResponse();
        try {
            wordsManager.addWords(request.getWords());
            this.success(response);
        } catch (BusinessException e) {
            logger.error(LogCategory.SEARCH, "添加分词发生异常:{}", e);
            this.error(response, e);
        }
        logger.debug(LogCategory.SEARCH, "添加分词响应信息:{}",response);
        return response;
    }

    @Path("indexoption")
    @POST
    @Override
    public IServiceResponse optionDocument(GoodsRequest request, HttpServletRequest servletRequest) {
        logger.debug(LogCategory.SEARCH, "处理索引请求信息:{}", request);

        // 初始化返回数据
        GeneralResponse response = new GeneralResponse();
        try {
            GoodsEO goodsEO = BeanMapper.map(request, GoodsEO.class);
            indexManager.optionDocument(request.getOptionType(), goodsEO);
            this.success(response);
        } catch (BusinessException e) {
            logger.error(LogCategory.SEARCH, "处理索引发生异常:{}", e);
            this.error(response, e);
        }
        logger.debug(LogCategory.SEARCH, "处理索引响应信息:{}",response);
        return response;
    }

}
